package com.yzd.otherlearn.jvm;

import com.yzd.otherlearn.util.Print;

public class FinalizeEscapeGC {
	public static FinalizeEscapeGC SAVE_HOOK = null;
	
	public void isAlive() {
		Print.print("yes i am still alive :)");
	}
	
	@Override
	protected void finalize() throws Throwable {
		super.finalize();
		Print.print("finalize method executed");
		FinalizeEscapeGC.SAVE_HOOK = this;
	}
	
	public static void main(String[] args) throws InterruptedException {
		SAVE_HOOK = new FinalizeEscapeGC();
		SAVE_HOOK = null;
		
		System.gc();
		
		Thread.sleep(500);
		if(SAVE_HOOK != null) {
			SAVE_HOOK.isAlive();
		} else {
			Print.print("no i am dead :(");
		}
		
		SAVE_HOOK = null ;
		System.gc();
		Thread.sleep(500);
		if(SAVE_HOOK != null) {
			SAVE_HOOK.isAlive();
		} else {
			Print.print("no i am dead :(");
		}
	}
}
